
#include "EditablePage.h"


void EditablePage::setFocus(bool focus) {

    if (mpeditUnits->empty()){
            return;
    }
    this->focus = focus;

    if (this->focus) {
        setValue();

        resetActiveEditor();
    } else if (nullptr != activeEditting){
        activeEditting->setFocus(false);
        activeEditting = nullptr;
        saveValue();
 //       refreshPage();
    }
}


void EditablePage::cancelEdit(){
    if (activeEditting){
        activeEditting->setFocus(false);
        activeEditting = nullptr;
    }
    this->focus = false;
}


void EditablePage::resetActiveEditor(){
    mcurrentBlinkIndex = 0;
    activeEditting = (*mpeditUnits)[mcurrentBlinkIndex];
    activeEditting->setFocus(true);
}



int EditablePage::onKeyDown(const KeyCode& keyCode) {
    int result;
    if (focus) {
        result = activeEditting->onKeyDown(keyCode);
    } else {
        result = super::onKeyDown(keyCode);
    }

    return result;
}



int EditablePage::onOtherKey(const KeyCode& keyCode) {
    int result;
    if (focus) {
        result = activeEditting->onOtherKey(keyCode);
    } else {
        result = super::onOtherKey(keyCode);
    }

    return result;
}



int EditablePage::onKeyUp(const KeyCode& keyCode) {
    int result;
    if (focus) {
        result = activeEditting->onKeyUp(keyCode);
    } else {
        result = super::onKeyUp(keyCode);
        mcurrentBlinkIndex = 0;
    }

    return result;
}

int EditablePage::onKeySet(const KeyCode& keyCode) {
    super::onKeySet(keyCode);
    if (focus) {
        activeEditting->setFocus(false);
        activeEditting->paint(true);
        mcurrentBlinkIndex = (1 + mcurrentBlinkIndex ) % mpeditUnits->size();
        activeEditting = (*mpeditUnits)[mcurrentBlinkIndex];
        activeEditting->setFocus(true);
    } else {
        setFocus(true);
    }

    return 2;//1
}

int EditablePage::onKeyEnt(const KeyCode& keyCode) {
    super::onKeyEnt(keyCode);

    setFocus(false);

    return 2;//1
}





